package com.he.controller;

import com.he.entity.Hotel;
import com.he.entity.ResultData;
import com.he.entity.Room;
import com.he.event.constact.EventConstact;
import com.he.event.util.EventUtil;
import com.he.service.IHotelService;
import com.he.service.IRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/hotel")
@Slf4j
public class HotelController {

    @Autowired
    private IHotelService hotelService;

    @Autowired
    private EventUtil eventUtil;

    /**
     * 查出当前酒店
     * @param hid
     * @return
     */
    @RequestMapping("/getHotelByHid/{hid}")
    Hotel getHotelByHid(@PathVariable Integer hid){
        return hotelService.getHotelByHid(hid);
    }

    /**
     * 添加酒店接口
     * @param hotel
     * @return
     */
    @RequestMapping("/save")
    public ResultData<Boolean> saveHotel(@RequestBody Hotel hotel){
        //log.info("添加酒店"+hotel);
        boolean flag = hotelService.save(hotel);
        return new ResultData<Boolean>().setData(flag);
    }

    /**
     * 酒店列表
     * @return
     */
    @RequestMapping("/list")
    public ResultData<List<Hotel>> hotelList(){
        List<Hotel> list = hotelService.list();
        log.info("酒店列表"+list);
        return new ResultData<List<Hotel>>().setData(list);
    }

    /**
     * 根据id查询酒店详情信息
     * @return
     */
    @RequestMapping("/getInfo")
    public ResultData<Hotel> getInfo(Integer hid){
        //查询酒店信息
        Hotel hotel = hotelService.getById(hid);
        log.info("点击的酒店为："+hotel);
        //TODO 发生了一次点击率 ????????????????
        eventUtil.publishEvent(EventConstact.EVENT_HOTEL_CLICK, hotel);

        return new ResultData<Hotel>().setData(hotel);
    }
}
